#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <irt_socket.h>
#include <nacl_socket.h>

void __sockaddr_to_nacl_abi_sockaddr(const struct sockaddr* srcaddr,
                                     socklen_t srclen,
                                     struct nacl_abi_sockaddr* dstaddr,
                                     size_t* dstlen) {
  memcpy(dstaddr, srcaddr, srclen);
  dstaddr->sa_family = __domain_to_nacl_abi_domain(srcaddr->sa_family);
  *dstlen = srclen;
}

void __nacl_abi_sockaddr_to_sockaddr(const struct nacl_abi_sockaddr* srcaddr,
                                     size_t srclen,
                                     struct sockaddr* dstaddr,
                                     socklen_t* dstlen) {
  memcpy(dstaddr, srcaddr, srclen);
  dstaddr->sa_family = __nacl_abi_domain_to_domain(srcaddr->sa_family);
  *dstlen = srclen;
}

int __nacl_abi_protocl_to_protocol(int nacl_abi_protocol) {
  switch(nacl_abi_protocol) {
    case NACL_SOCK_STREAM: return SOCK_STREAM;
    case NACL_SOCK_DGRAM: return SOCK_DGRAM;
  };

  return -1;
}

int __protocol_to_nacl_abi_protocol(int protocol) {
  switch(protocol) {
    case SOCK_STREAM: return NACL_SOCK_STREAM;
    case SOCK_DGRAM: return NACL_SOCK_DGRAM;
  };

  return -1;
}

int __nacl_abi_domain_to_domain(int nacl_abi_domain) {
  switch(nacl_abi_domain) {
    case NACL_AF_INET: return AF_INET;
    case NACL_AF_INET6: return AF_INET6;
  }

  return -1;
}

int __domain_to_nacl_abi_domain(int domain) {
  switch(domain) {
    case AF_INET: return NACL_AF_INET;
    case AF_INET6: return NACL_AF_INET6;
  }

  return -1;
}